//
//  QLCicleOfFriendCommentView.m
//  神约
//
//  Created by qlwl on 16/12/14.
//  Copyright © 2016年 qlwl. All rights reserved.
//

#import "QLCicleOfFriendCommentView.h"
#import "UIView+FrameKIt.h"
#import "QLCommentOfFriendCell.h"

@implementation QLCicleOfFriendCommentView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initUI];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

- (void)initUI
{
    self.clipsToBounds = YES;
    _bgIma = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"cicleCommentBg"] resizableImageWithCapInsets:UIEdgeInsetsMake(16,40,4,4) resizingMode:UIImageResizingModeStretch]];
    [self addSubview:_bgIma];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.scrollEnabled = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self addSubview:self.tableView];
}


- (void)setCommentArray:(NSArray<TYTextContainer *> *)commentArray
{
    _commentArray = commentArray;
    [self.tableView reloadData];
}
//- (void)setPrisePeople:(TYTextContainer *)prisePeople
//{
//    _prisePeople = prisePeople;
//    [self.tableView reloadData];
//}
- (void)refreshHeightWithWidth:(float)width
{
    if (self.prisePeople) {
        [_commentArray insertObject:self.prisePeople atIndex:0];
    }
    self.f_width = width;
    float height = [self getCommentHeightWithWidth:self.f_width];
    self.f_height = height?height + 6:0;
    
    _bgIma.frame = self.bounds;
    _tableView.f_top = 6;
    _tableView.f_left = 0;
    _tableView.f_width = _bgIma.f_width;
    _tableView.f_height = _bgIma.f_height - _tableView.f_top;
    [self.tableView reloadData];
}
- (float)getCommentHeightWithWidth:(float)width
{
    float commentWidth = width -  6 - 1;//距离左右6
    float height = 0;
    for (TYTextContainer * textContainer in _commentArray) {
        height += [textContainer getHeightWithFramesetter:nil width:commentWidth];
        height += 2;// 距离上下2
    }
    return height;
}


#pragma mark tableView 

#pragma mark TabDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellName = @"cell";
    QLCommentOfFriendCell * cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil) {
        cell = [[QLCommentOfFriendCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
    }
    cell.commentLab.textContainer = self.commentArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    float commentWidth = self.f_width - 6 - 1;//距离左右6
    TYTextContainer * textContainer = _commentArray[indexPath.row];
    float height = [textContainer getHeightWithFramesetter:nil width:commentWidth];
    return height + 2;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    if (self.commentItemClick) {
        self.commentItemClick(indexPath,[cell convertRect:cell.bounds toView:window].origin.y + cell.f_height);
    }
}

@end
